<?php
if (!(defined('IN_IA')))
{
    exit('Access Denied');
}

class Home extends WeModuleWxapp
{	public function fwb_del_font($str)		{			$str = htmlspecialchars_decode($str);			$re = preg_replace("/font-family:.*;/i",'',$str);			$re = preg_replace("/font-family:.*\"/i",'"',$re);			return $re;		}
    /**
     * 获取轮播
     */
	public function Adlist() {		global $_W,$_GPC;		$data = pdo_getall('bd_cms_ad', array('uniacid'=>$_W['uniacid'],'is_show'=>1),'', 'order By add_time desc');		foreach($data as &$ad){			$ad['ad_thumb']=tomedia($ad['ad_thumb']);		}		$this->result(0, '广告列表', $data);	}		/**	 *  获取文章列表	 */	public function Articlelist() {		global $_W, $_GPC;		$query = load()->object('query');		$article_list=$query->from('bd_cms_article')->where(['uniacid'=>$_W['uniacid'],'is_show'=>1]);	    if($_GPC['article_title']){             $article_list = $article_list->where(' article_title like',"%{$_GPC['article_title']}%");        }	    if($_GPC['article_cat']){             $article_list = $article_list->where(['article_cat'=>$_GPC['article_cat']]);        }        $beginPage=0;        if($_GPC['page']>=1){        	$beginPage=$_GPC['page'];        }        $size=10;        if($_GPC['size']){        	$size=$_GPC['size'];        }        $begin=$beginPage*$size;                $article_list = $article_list->limit($begin,$size);		$article_list=$article_list->orderby('article_sort', 'DESC')->getall();		foreach($article_list as &$ad){			$ad['article_thumb']=tomedia($ad['article_thumb']);			$ad['article_content']=$this->fwb_del_font($ad['article_content']);		}		$this->result(0, '文章列表', $article_list);	}	/**	 *  获取公告详情	 */	public function Articleinfo() {		global $_W, $_GPC;		$id = intval($_GPC['article_id']);		$data = pdo_get('bd_cms_article', array('article_id'=>$id, 'uniacid'=>$_W['uniacid']));		$data['article_thumb']=tomedia($data['article_thumb']);		$res=pdo_get('bd_favorites', array('fav_type'=>'article','type_key'=>$id,'uniacid'=>$_W['uniacid'],'open_id'=>$_W['openid']));		$data['IsFavorite']=0;		if($res){			$data['IsFavorite']=1;		}		$this->result(0, '获取文章单条日记',$data);	}			public function Typelist() {		global $_W,$_GPC;		$data = pdo_getall('bd_mis_product_type', array('uniacid'=>$_W['uniacid']),'', 'order By add_time desc');		foreach($data as &$ad){			$ad['type_logo']=tomedia($ad['type_logo']);			$ad['products']=pdo_getall('bd_mis_product', array('uniacid'=>$_W['uniacid'],'product_type'=>$ad['type_id']));			foreach($ad['products'] as &$pro){				$pro['product_thumb']=tomedia($pro['product_thumb']);			}					}		$this->result(0, '产品类别列表', $data);	}		/**获取产品列表  */	public function Productlist(){		global $_W,$_GPC;		$query = load()->object('query');		$product_list=$query->from('bd_mis_product')->where(['uniacid'=>$_W['uniacid'],'is_show'=>1]);	    if($_GPC['product_name']){             $product_list = $product_list->where(' product_name like',"%{$_GPC['product_name']}%");        }	    if($_GPC['product_type']){             $product_list = $product_list->where(['product_type'=>$_GPC['product_type']]);        }        $beginPage=0;        if($_GPC['page']>=1){        	$beginPage=$_GPC['page'];        }        $size=10;        if($_GPC['size']){        	$size=$_GPC['size'];        }                $begin=$beginPage*$size;                $product_list = $product_list->limit($begin,$size);		$product_list=$product_list->orderby('add_time', 'DESC')->getall();		foreach($product_list as &$ad){			$ad['product_thumb']=tomedia($ad['product_thumb']);		}		$this->result(0, 'ok',$product_list);			}	/**	 *  获取产品详情	 */	public function Productinfo() {		global $_W,$_GPC;		$id = intval($_GPC['product_id']);		$data = pdo_get('bd_mis_product', array('product_id'=>$id, 'uniacid'=>$_W['uniacid']));		$data['product_thumb']=tomedia($data['product_thumb']);		$this->result(0, '产品信息',$data);	}			public function Configlist(){		global $_W, $_GPC;		$config_group='enterprise';		if($_GPC['config_group']){			$config_group=$_GPC['config_group'];		}			$list=pdo_getall('bd_sys_config',array('uniacid'=>$_W['uniacid'],'config_group'=>$config_group,'status'=>1));			$config_row=array();		foreach ($list as &$conf){			$config_row[$conf['config_code']]=$conf['config_value'];		}		$config_row['enterprise_pic']=tomedia($config_row['enterprise_pic']);		$config_row['enterprise_logo']=tomedia($config_row['enterprise_logo']);		$config_row['enterprise_qrcode']=tomedia($config_row['enterprise_qrcode']);		$config_row['config_group']=$config_group;		$this->result(0, '公司配置内容', $config_row);	}		/**	 *  收藏列表	 */	public function Favoritelist() {		global $_W,$_GPC;		$query = load()->object('query');		$fav_list=$query->from('bd_favorites')->where(['uniacid'=>$_W['uniacid'],'open_id'=>$_W['openid']]);		$beginPage=0;		if($_GPC['page']>=1){			$beginPage=$_GPC['page'];		}		$size=10;		if($_GPC['size']){			$size=$_GPC['size'];		}				$begin=$beginPage*$size;				$fav_list = $fav_list->limit($begin,$size);		$fav_list=$fav_list->orderby('add_time', 'DESC')->getall();		$this->result(0, 'ok',$fav_list);	}		/**	 *  添加和取消收藏	 */	public function Favoritedo() {		global $_W, $_GPC;		$fav_type = $_GPC['fav_type'];		$type_key = $_GPC['type_key'];		$IsFavorite = $_GPC['IsFavorite'];		$open_id = $_W['openid'];		if($IsFavorite==1){			$res = pdo_insert('bd_favorites', array('fav_type'=>$fav_type, 'type_key'=>$type_key,'open_id'=>$open_id,					'add_time'=>date('Y-m-d H:i:s'), 'last_time'=>date('Y-m-d H:i:s'),					'uniacid'=>$_W['uniacid'],'uid'=>$_W['uid']));			$message='收藏成功';		}else{			$res = pdo_delete('bd_favorites', array('fav_type'=>$fav_type, 'type_key'=>$type_key,'open_id'=>$open_id));			$message='收藏已取消';		}				if ($res) {			$this->result(0, $message, $res);return;		}else{			$this->result(0, '添加失败');		}	}																																																	
    /**
     * 获取公司信息
     */
    public function getCompany(){
        global $_W,$_GPC;
        $query = load()->object('query');
        $detail = $query->from('miniapp_company_info')
            ->select(['name','logo','x','y','address','description',
                'phone','appointment_img','videos','contact',
                'aot1','aot2','aot3','aot4','right_img','right_text','news_title','product_title'])
            ->where(['store_id' => $_W['uniacid']])
            ->get();
        $notice= $query->from('miniapp_company_notice')
            ->select(['title'])
            ->where(['store_id' => $_W['uniacid']])
            ->getall();
        if($detail){
            $detail['notice'] = $notice?$notice:['title'=>'小程序上线了！'];
            $detail['logo'] = tomedia($detail['logo']);
            $detail['appointment_img'] = tomedia($detail['appointment_img']);
            $detail['videos'] = tomedia($detail['videos']);
            $detail['right_img'] = tomedia($detail['right_img']);
            $detail['description'] = htmlspecialchars_decode($detail['description']);
            return $this->result(0, '', $detail);
        }else{
            return $this->result(12, '数据为空', '');
        }
    }
    /**
     * 获取公司信息
     */
    public function getModules(){
        global $_W,$_GPC;
        $query = load()->object('query');
        $detail = $query->from('miniapp_company_info')
            ->select(['start_img','modules'])
            ->where(['store_id' => $_W['uniacid']])
            ->get();
        if($detail){
            $detail['start_img'] = tomedia($detail['start_img']);
            return $this->result(0, '', $detail);
        }else{
            return $this->result(0, '数据为空', ['start_img'=>tomedia('/addons/miniapp_company/static/images/timg.jpg'),'modules'=>'base']);
        }
    }


    /**
     * 获取轮播
     */
    public function getProductType(){
        global $_W,$_GPC;
        $query = load()->object('query');
        $list = $query->from('miniapp_company_product_type')
            ->select(['id','name','img','sort'])
            ->where(['store_id' => $_W['uniacid']])
            ->orderby('sort', 'DESC')
            ->limit($_GPC['num'] ?: 4)
            ->getall();
        if($list){
            foreach ($list as $k => $v){
                $list[$k]['img'] = tomedia($v['img']);
            }
            return $this->result(0, '', $list);
        }else{
            return $this->result(0, '数据为空', '');
        }
    }
    /**
     * 获取轮播
     */
    public function getApp(){
        global $_W,$_GPC;
        $query = load()->object('query');
        $list = $query->from('miniapp_company_miniprogram')
            ->select(['appid','path','icon','name'])
            ->where(['uniacid' => $_W['uniacid']])
            ->orderby('id', 'DESC')
            ->limit($_GPC['num'] ?: 4)
            ->getall();
        if($list){
            foreach ($list as $k => $v){
                $list[$k]['icon'] = tomedia($v['icon']);
            }
            return $this->result(0, '', $list);
        }else{
            return $this->result(0, '数据为空', '');
        }
    }
    /**
     * 获取轮播
     */
    public function Product(){
        global $_W,$_GPC;
        $query = load()->object('query');
        $list = $query->from('miniapp_company_product')
            ->select(['id','name','title','price','small_image','sort'])
            ->where(['store_id' => $_W['uniacid']])
            ->orderby('sort', 'DESC')
            ->limit($_GPC['num'] ?: 10)
            ->getall();
        if($list){
            foreach ($list as $k => $v){
                $list[$k]['small_image'] = tomedia($v['small_image']);
            }
            return $this->result(0, '', $list);
        }else{
            return $this->result(0, '数据为空', []);
        }
    }

    /**
     * 获取资讯
     */
    public function News(){
        global $_W,$_GPC;
        $query = load()->object('query');
        $list = $query->from('miniapp_company_news')
            ->select(['id','title','image','create_time'])
            ->where(['store_id' => $_W['uniacid']])
            ->orderby('id', 'DESC')
            ->limit($_GPC['num'] ?: 10)
            ->getall();
        if($list){
            foreach ($list as $k => $v){
                $list[$k]['image'] = tomedia($v['image']);
                $list[$k]['create_time'] = date("m月d日 ",$v['create_time']);
            }
            return $this->result(0, '', $list);
        }else{
            return $this->result(0, '数据为空', []);
        }
    }

    /**
     * 预约表单处理
     */
    public function setAppoint(){
        global $_W,$_GPC;
        $params = [
            'admin_id' => $_W['uniacid'],
            'appoint_time' => $_SERVER['REQUEST_TIME']
        ];
        if($_GPC['aot1'] && $_GPC['aot1'] != 'undefined'){
            $params['name'] = $_GPC['aot1'];
        }
        if($_GPC['aot2'] && $_GPC['aot2'] != 'undefined'){
            $params['phone'] = $_GPC['aot2'];
        }
        if($_GPC['aot3'] && $_GPC['aot3'] != 'undefined'){
            $params['sex'] = $_GPC['aot3'];
        }
        if($_GPC['aot4'] && $_GPC['aot4'] != 'undefined'){
            $params['email'] = $_GPC['aot4'];
        }
        $res=pdo_insert('miniapp_company_appoint',$params);
        if($res){
            $this->sms_send();
            return $this->result(0, '提交成功', '');
        }else{
            return $this->result(4, '提交失败', '');
        }

    }

    public function getTabBar(){
        global $_W;
        $query = load()->object('query');
        $detail = $query->from('miniapp_company_tabbar')
            ->select(['m1_name','m2_name','m3_name','m4_name','m1_img','m2_img',
                'm3_img','m4_img','m1_selimg','m2_selimg','m3_selimg','m4_selimg',
                'border_color','background_color','color','sel_color'])
            ->where(['uniacid' => $_W['uniacid'],'on_off' => '1'])
            ->get();
        if($detail){
            $detail['m1_img'] = tomedia($detail['m1_img']);
            $detail['m2_img'] = tomedia($detail['m2_img']);
            $detail['m3_img'] = tomedia($detail['m3_img']);
            $detail['m4_img'] = tomedia($detail['m4_img']);
            $detail['m1_selimg'] = tomedia($detail['m1_selimg']);
            $detail['m2_selimg'] = tomedia($detail['m2_selimg']);
            $detail['m3_selimg'] = tomedia($detail['m3_selimg']);
            $detail['m4_selimg'] = tomedia($detail['m4_selimg']);
            return $this->result(0, '', $detail);
        }else{
            return $this->result(0, '', '');
        }

    }
    public function sms_send()
    {
        global $_W;
        $result=pdo_fetch("SELECT * FROM ".tablename('miniapp_company_sms')." where `store_id`={$_W['uniacid']}order by id desc limit 1");
        if($result['type']==1)
        {
            $this->ali_sms($result['appid'], $result['app_key'], $result['phone'], $result['sign'], $result['templId'], $result['params']);
        }
        else if($result['type']==2)
        {
            $this->tx_sms($result['appid'], $result['app_key'], $result['phone'], $result['sign'], $result['templId'], $result['params']);
        }
    }
    /*
     * 阿里云发送短信
     */
    public function ali_sms($accessKeyId, $accessKeySecret, $phone, $SignName, $TemplateCode, $name)
    {
        /* 阿里 */
        $Ali_sms = m('smsali');
        $domain = 'dysmsapi.aliyuncs.com';
        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $phone;
        // fixme 必填: 短信签名，莫网微官网
        $params["SignName"] = $SignName;
        // fixme 必填: 短信模板Code，SMS_121111111
        $params["TemplateCode"] = $TemplateCode;
        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = Array (
            "name" => $name,
            "time" => date('H:i',time()),
            );
        // fixme 可选: 设置发送短信流水号
        //$params['OutId'] = "12346";
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }
        $params['RegionId'] = "cn-hangzhou";
        $params['Action'] = "SendSms";
        $params['Version'] = "2017-05-25";
    
        $res =  $Ali_sms->request($accessKeyId,$accessKeySecret,$domain,$params);
        /* echo '<pre>';
         var_dump(json_decode($res,true));
         exit; */
    }
    /*
     * 腾讯云短信
     */
    public function tx_sms($appid, $appkey, $phone, $sign, $templId, $name)
    {
        m('smstx')->init($appid, $appkey);
        $phone = $phone;
        $templId = $templId;//模板id  83251
        $params = [$name,date('H:i',time())];//模板自定义信息替换
        $sign = $sign;//签名  莫奈网络
        $res = m('smstx')->sendWithParam(86, $phone, $templId, $params, $sign);
        //var_dump(json_decode($res,true));exit;
    }


    /**
     * 获取分类导航图标默认四个
     */
    public function getNavigation()
    {
        global $_W,$_GPC;
        $query = load()->object('query');
        $detail = $query->from('miniapp_company_news_type')
            ->select(['id','img','name'])
            ->where(['store_id' => $_W['uniacid'],'class_type'=>0])
            ->orderby('sort', 'DESC')
            ->limit(4)
            ->getall();
        foreach($detail as &$d){
            $d['img']=tomedia($d['img']);
        }
        return $this->result(0, '', $detail);
    }
    /**
     * 获取首页的新闻
     */
    public function getIndexNews(){
        global $_W,$_GPC;
        $query = load()->object('query');
        //获取分类中倒序排序后的最前两个分类的名称与id
        $type = $query->from('miniapp_company_news_type')
            ->select(['id','name'])
            ->where(['store_id' => $_W['uniacid'],'type'=>1,'class_type'=>0])
            ->orderby('sort', 'DESC')
            ->limit(4,2)
            ->getall();
        //分别获取这两个id对应的新闻列表，第一个id为第一层的，仅查询4条，第二个id为第二层，查询6条

        $firstnews['classname']='';
        $firstnews['type']='';
        $firstnews['datas']=[];
        if($type && $type[0]){
            $firstnews['classname']=$type[0]['name'];
            $firstnews['type']=$type[0]['id'];
            $firstnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','title','image','create_time'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[0]['id']])
                ->orderby('id', 'DESC')
                ->limit(4)
                ->getall();
            foreach($firstnews['datas'] as &$f){
                $f['create_time']=date('m-d',intval($f['create_time'])?intval($f['create_time']):time());
            }
        }
        $secondnews['classname']='';
        $secondnews['type']='';
        $secondnews['datas']=[];
        if($type && $type[1]){
            $secondnews['classname']=$type[1]['name'];
            $secondnews['type']=$type[1]['id'];
            $secondnews['classname']=$type[1]['name'];
            $secondnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','title','image','create_time'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[1]['id']])
                ->orderby('id', 'DESC')
                ->limit(6)
                ->getall();
            foreach($secondnews['datas'] as &$f){
                $f['create_time']=date('Y-m-d',intval($f['create_time'])?intval($f['create_time']):time());
            }
        }
        $re=[
            'firstnews'=>$firstnews,
            'secondnews'=>$secondnews
        ];
        return $this->result(0, '', $re);

    }
    /**
     * 获取第二页的新闻
     */
    public function getSecondeNews(){
        global $_W,$_GPC;
        $query = load()->object('query');
        //获取分类中倒序排序后的最前两个分类的名称与id
        $type = $query->from('miniapp_company_news_type')
            ->select(['id','name'])
            ->where(['store_id' => $_W['uniacid'],'type'=>1,'class_type'=>0])
            ->orderby('sort', 'DESC')
            ->limit(2,3)
            ->getall();
        //分别获取这两个id对应的新闻列表，第一个id为第一层的，仅查询4条，第二个id为第二层，查询6条
        $video = $query->from('miniapp_company_news_type')
            ->select(['id','name'])
            ->where(['store_id' => $_W['uniacid'],'type'=>2,'class_type'=>0])
            ->get();
        $firstnews['classname']='';
        $firstnews['type']='';
        $firstnews['datas']=[];
        if($video && $video['id']){
            $firstnews['classname']=$video['name'];
            $firstnews['type']=$video['id'];
            $firstnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','video'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$video['id']])
                ->orderby('id', 'DESC')
                ->limit(6)
                ->getall();
            foreach($firstnews['datas'] as &$f){
                $f['video']=tomedia($f['video']);
            }
        }
        $secondnews['classname']='';
        $secondnews['type']='';
        $secondnews['datas']=[];
        if($type && $type[0]){
            $secondnews['classname']=$type[0]['name'];
            $secondnews['type']=$type[0]['id'];
            $secondnews['classname']=$type[0]['name'];
            $secondnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','title','create_time'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[0]['id']])
                ->orderby('id', 'DESC')
                ->limit(4)
                ->getall();
            foreach($secondnews['datas'] as &$f){
                $f['create_time']=date('m-d',intval($f['create_time'])?intval($f['create_time']):time());
            }
        }
        $thirdnews['classname']='';
        $thirdnews['type']='';
        $thirdnews['datas']=[];
        if($type && $type[1]){
            $thirdnews['classname']=$type[1]['name'];
            $thirdnews['type']=$type[1]['id'];
            $thirdnews['classname']=$type[1]['name'];
            $thirdnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','title','create_time'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[1]['id']])
                ->orderby('id', 'DESC')
                ->limit(4)
                ->getall();
            foreach($thirdnews['datas'] as &$f){
                $f['create_time']=date('m-d',intval($f['create_time'])?intval($f['create_time']):time());
            }
        }
        $re=[
            'firstnews'=>$firstnews,
            'secondnews'=>$secondnews,
            'thirdnews'=>$thirdnews
        ];
        return $this->result(0, '', $re);

    }
    /**
     * 获取第三页的新闻
     */
    public function getThirdNews(){
        global $_W,$_GPC;
        $query = load()->object('query');
        $info=$query->from('miniapp_company_info')
            ->select(['id','name','logo'])
            ->where(['store_id' => $_W['uniacid']])
            ->get();
        $info['logo']=tomedia($info['logo']);
        //获取分类中倒序排序后的最前两个分类的名称与id
        $type = $query->from('miniapp_company_news_type')
            ->select(['id','name'])
            ->where(['store_id' => $_W['uniacid'],'type'=>1,'class_type'=>1])
            ->orderby('sort', 'DESC')
            ->limit(0,2)
            ->getall();
        $firstnews['classname']='';
        $firstnews['type']='';
        $firstnews['datas']=[];
        if($type && $type[0]['id']){
            $firstnews['classname']=$type[0]['name'];
            $firstnews['type']=$type[0]['id'];
            $firstnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','title'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[0]['id']])
                ->orderby('id', 'DESC')
                ->limit(24)
                ->getall();
        }
        $secondnews['classname']='';
        $secondnews['type']='';
        $secondnews['datas']=[];
        if($type && $type[1]){
            $secondnews['classname']=$type[1]['name'];
            $secondnews['type']=$type[1]['id'];
            $secondnews['classname']=$type[1]['name'];
            $secondnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','title'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[1]['id']])
                ->orderby('id', 'DESC')
                ->limit(24)
                ->getall();
        }
        $re=[
            'firstnews'=>$firstnews,
            'secondnews'=>$secondnews,
            'info'=>$info
        ];
        return $this->result(0, '', $re);

    }
    /**
     * 获取第四页的新闻
     */
    public function getFourthNews(){
        global $_W,$_GPC;
        $query = load()->object('query');
        //获取分类中倒序排序后的最前两个分类的名称与id
        $type = $query->from('miniapp_company_news_type')
            ->select(['id','name'])
            ->where(['store_id' => $_W['uniacid'],'type'=>1,'class_type'=>2])
            ->orderby('sort', 'DESC')
            ->limit(0,4)
            ->getall();
        $firstnews['classname']='';
        $firstnews['type']='';
        $firstnews['datas']=[];
        if($type && $type[0]['id']){
            $firstnews['classname']=$type[0]['name'];
            $firstnews['type']=$type[0]['id'];
            $firstnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','image','content'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[0]['id']])
                ->get();
            $firstnews['datas']['content'] = htmlspecialchars_decode($firstnews['datas']['content']);
            $firstnews['datas']['image']=tomedia($firstnews['datas']['image']);

        }
        $secondnews['classname']='';
        $secondnews['type']='';
        $secondnews['datas']=[];
        if($type && $type[1]){
            $secondnews['classname']=$type[1]['name'];
            $secondnews['type']=$type[1]['id'];
            $secondnews['classname']=$type[1]['name'];
            $secondnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','title','image','create_time'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[1]['id']])
                ->orderby('id', 'DESC')
                ->limit(4)
                ->getall();
            foreach($secondnews['datas'] as &$f){
                $f['image']=tomedia($f['image']);
            }
        }
        $thirdnews['classname']='';
        $thirdnews['type']='';
        $thirdnews['datas']=[];
        if($type && $type[2]){
            $thirdnews['classname']=$type[2]['name'];
            $thirdnews['type']=$type[2]['id'];
            $thirdnews['classname']=$type[2]['name'];
            $thirdnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','title','image','create_time'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[2]['id']])
                ->orderby('id', 'DESC')
                ->limit(4)
                ->getall();
            foreach($thirdnews['datas'] as &$f){
                $f['image']=tomedia($f['image']);
            }
        }
        $fourthnews['classname']='';
        $fourthnews['type']='';
        $fourthnews['datas']=[];
        if($type && $type[3]){
            $fourthnews['classname']=$type[3]['name'];
            $fourthnews['type']=$type[3]['id'];
            $fourthnews['classname']=$type[3]['name'];
            $fourthnews['datas']=$query->from('miniapp_company_news')
                ->select(['id','title','image','create_time'])
                ->where(['store_id' => $_W['uniacid'],'type'=>$type[3]['id']])
                ->orderby('id', 'DESC')
                ->limit(4)
                ->getall();
            foreach($fourthnews['datas'] as &$f){
                $f['image']=tomedia($f['image']);
            }
        }
        $re=[
            'firstnews'=>$firstnews,
            'secondnews'=>$secondnews,
            'thirdnews'=>$thirdnews,
            'fourthnews'=>$fourthnews
        ];
        return $this->result(0, '', $re);

    }
    /**
     * 获取资讯
     */
    public function NewsList(){
        global $_W,$_GPC;
        $size = 4;
        $beginPage = ($_GPC['page']-1)*$size;
        $query = load()->object('query');
        $type['type']=1;
        $type['name']='咨询列表';
        if($_GPC['type'] && $_GPC['type'] != 'undefined'){
            $type=$query->from('miniapp_company_news_type')
                ->select(['type','name'])
                ->where(['store_id' => $_W['uniacid'],'id'=>$_GPC['type']])
                ->get();
        }
        $list = $query->from('miniapp_company_news')
            ->select(['id','title','image','video','create_time'])
            ->where(['store_id' => $_W['uniacid']])
            ->orderby('id', 'DESC');
        if($_GPC['type'] && $_GPC['type'] != 'undefined'){
            $list = $list->where(['type'=>$_GPC['type']]);
        }
        if($beginPage < 1){
            $list = $list->limit($size);
        }else{
            $list = $list->limit($beginPage,$size);
        }
        if($_GPC['key'] && $_GPC['key'] != 'undefined'){
            $list = $list->where('title like',"%{$_GPC['key']}%");
        }
        $list = $list->getall();

            foreach ($list as $k => $v){
                $list[$k]['image'] = tomedia($v['image']);
                $list[$k]['video'] = tomedia($v['video']);
                $list[$k]['create_time'] = date("Y-m-d ",$v['create_time']);
            }
            $re=[
                'list'=>$list,
                'type'=>$type['type'],
                'name'=>$type['name'],
            ];
            return $this->result(0, '', $re);

    }
    //获取小程序列表
    public function getwxapp()
    {
        global $_W,$_GPC;
        $query = load()->object('query');
            $re=$query->from('miniapp_company_miniprogram')
                ->select(['appid','path','icon','name'])
                ->where(['uniacid' => $_W['uniacid']])
                ->getall();
            foreach($re as &$r){
                $r['icon']=tomedia($r['icon']);
            }
        return $this->result(0, '', $re);
    }
}
?>